package com.fdb.efp.cus.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gdrcu.efp.cus.common.CompanyStatusEnum;
import com.gdrcu.efp.cus.common.CompanyTypeEnum;
import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.efp.cus.service.vo.CompanyWhiteListVO;
import com.fdb.efp.esb.service.bo.req.yed.YedCompanyInfoRequestBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedCompanyInfoReqBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedCompanyResultReqBean;
import com.fdb.efp.esb.service.bo.resp.yed.YedPengYuanCompanyInfoRespBean;
import com.fdb.efp.esb.service.facade.yed.YedCompanyInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.fdb.efp.cus.service.facade.YedAutoDelComWhiteService;
import com.fdb.efp.cus.service.facade.CompanyWhiteListService;
import java.util.*;
import com.fdb.basic.framework.core.base.FrameworkService;

/**
 * TODO: <br>
 * <p>
 * 0.1:caoxin:2020/9/24 16:48: TODO: <br>
 *
 * @author caoxin
 * @version 0.1
 * @date 2020/9/24 16:48
 * @since 2020/9/24 16:48
 */
@Service("yedAutoDelComWhiteService")
public class YedAutoDelComWhiteServiceImpl  implements YedAutoDelComWhiteService{

    private static Logger logger = LoggerFactory.getLogger(CompanyWhiteListServiceImpl.class);

    @Autowired
    @Qualifier("companyWhiteListService")
    private CompanyWhiteListService companyWhiteListService;

    @Autowired
    @Qualifier("yedCompanyInfoService")
    YedCompanyInfoService yedCompanyInfoService;


    @Override
    public void deleteComWhite(){
        try{
            //先查询有效期到期的数
            List<CompanyWhiteListVO> list = companyWhiteListService.queryOverDtList();
            if (list.size()>0){//调用鹏元数据
                for (CompanyWhiteListVO item:list){
                    YedCompanyInfoRequestBean requestBean = new YedCompanyInfoRequestBean();
                    requestBean.setEntpNm(item.getCompanyName());
                    YedCompanyInfoReqBean yedCompanyInfoReqBean = yedCompanyInfoService.queryCompanyInfoForIOP(requestBean);
                    if (yedCompanyInfoReqBean !=null){
                        Map resultReqBean = yedCompanyInfoReqBean.getQryRsltArry().get(0);
                        //判断企业状态是否正常
                        //for (Map resultReqBean : qryRsltArry) {
                        Object entpBscInfStrct = resultReqBean.get("EntpBscInfStrct");
                        String entpBscInfStrctString = JSONObject.toJSONString(entpBscInfStrct);
                        YedPengYuanCompanyInfoRespBean res = JSONObject.parseObject(entpBscInfStrctString, YedPengYuanCompanyInfoRespBean.class);
                        String entpNm = res.getEntpNm();//企业名称
                        String entpSt = res.getEntpSt();//企业状态

                        if(!CompanyStatusEnum.ZAI_YING.getKey().equals(entpSt)){//企业状态非正常则删除
                            companyWhiteListService.deleteByCompName(item);
                        }else{//正常则更新
                            String currDt = TimeUtil.getCurrentDateTime();
                            String currDt1 = currDt.substring(0,10);
                            String currDt2 = currDt.substring(11);

                            String entpTp = res.getEntpTp();
                            String companyType = CompanyTypeEnum.parseKey(entpTp);
                            if (companyType.contains("国有")) {
                                companyType = "3";
                            } else if (companyType.contains("事业")) {
                                companyType = "2";
                            } else {
                                companyType = "8";
                            }
                            item.setCompanyType(companyType);
                            item.setUpdateName("系统");
                            item.setUpdateNo("System");
                            item.setUpdateDate(currDt1);
                            item.setUpdateTime(currDt2);
                            if(!"".equals(res.getRgstrnTm()) && res.getRgstrnTm()!=null){
                                item.setRegisterTime(res.getRgstrnTm().substring(0,4)+"-"+res.getRgstrnTm().substring(4,6)+"-"+res.getRgstrnTm().substring(6,8));
                            }
                            item.setStatus(entpSt);
                            item.setEffectiveDate(0);

                            companyWhiteListService.updateByCompName(item);
                        }
                        //}
                    }else{//不存在则删除
                        companyWhiteListService.deleteByCompName(item);
                    }
                }
            }
        }catch (Exception e) {
            logger.error("企业白名单自动更新失败!", e);
        }
    }
}
